package com.shengruan.index.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.RequestDispatcher;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.vo.Product;
import com.shengruan.admin.vo.ProductClass;
import com.shengruan.admin.vo.User;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Constants;
import com.shengruan.user.action.AutoAuctionUtil;

public class IndexAction extends BaseAction {

	Logger logger = LoggerFactory.getLogger(getClass());
	@Override
	public void execute() throws Exception {

		User loginUser = this.getCurrentUser();
		Integer classId = 0;
		Integer status = 0;
		try {
			classId = Integer.parseInt(this.input("c"));
		}
		catch (Exception ex) {

		}
		try {
			status = Integer.parseInt(this.input("s"));
		}
		catch (Exception ex) {

		}
		List<ProductClass> productClassList = Constants.productClassList;

		StringBuffer idSb = new StringBuffer();
		List<Product> productList = new ArrayList<Product>();
		Set<Integer> keySet = AuctionUtil.productMap.keySet();
		Iterator<Integer> iterator = keySet.iterator();
		while (iterator.hasNext()) {
			Product product = (Product) AuctionUtil.productMap.get((Integer) iterator.next());
			
			Product newProduct = new Product();
			BeanUtils.copyProperties(newProduct, product);
			
			if(loginUser!=null)
			{
				if(loginUser.getId()==newProduct.getBidUserId())
				{
					newProduct.setBidUserName("我");
				}
				if(AutoAuctionUtil.get(loginUser.getId()+"||"+newProduct.getId())!=null)
				{
					newProduct.setAutoIf(1L);
				}
				
			}
			if (classId == null || classId.intValue() == 0) {
				if(status==0||status==newProduct.getAuctionStatus())
				{
					idSb.append(product.getId() + ",");
					productList.add(newProduct);
				}
			} else {
				if (newProduct.getClassId() == classId) {
					if(status==0||status==newProduct.getAuctionStatus())
					{
						idSb.append(newProduct.getId() + ",");
						productList.add(newProduct);
					}
				}
			}
		}
		String lastIdStr = "";
		if (idSb.length() > 0) {
			lastIdStr = idSb.substring(0, idSb.length() - 1);
		}
		
		  ProductComparator comp = new ProductComparator();  

		  Collections.sort(productList,comp);


		    
		this.setRequest("classId", classId);
		this.setRequest("status", status);
		this.setRequest("productList", productList);
		this.setRequest("productClassList", productClassList);
		this.setRequest("lastIdStr", lastIdStr);
		this.setRequest("sliderLogoList", Constants.sliderLogoList);
		this.setRequest("serverTime", System.currentTimeMillis());
        
        RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
        try {
            rd.forward(request, response);
            return;
        }catch(Exception e){
            e.printStackTrace();
        }


	}

	/**
	 * 加载右边页面
	 * @throws Exception
	 */
	public void doRight() throws Exception {
        this.setRequest("USER_INVITEMAXBIDDING", Constants.USER_INVITEMAXBIDDING);
        this.forward("right.jsp");
    }

	/***
	 * 获得服务器时间
	 */
	public void getServerTime() throws Exception
	{
		this.print(System.currentTimeMillis()+"");
	}
}
